/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.mrp.pmm.entity;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import merlion.common.util.Consts.ProductType;
import merlion.scm.icm.entity.InventoryReservation;
import merlion.scm.icm.entity.ProductInventoryRecord;
import merlion.scm.icm.entity.WarehouseLocation;

/**
 *
 * @author Zhang Ying, Zhu Xin
 */
@Entity
public class FinishedGood implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;  
    private String batchNo;
    private Long quantity;
    private Timestamp dateManufactured;
    private Timestamp dateShelf;
    private Timestamp datePrinted;
    private Timestamp dateExpired;
    private Double basePrice;
    @ManyToOne(targetEntity = ProductInventoryRecord.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    private ProductInventoryRecord inventory;
    @OneToMany(targetEntity = InventoryReservation.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    private List<InventoryReservation> reservations;
    @OneToMany(targetEntity = WarehouseLocation.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    private List<WarehouseLocation> locations;
    
    public FinishedGood() {
        batchNo = generateBatchNo();
        quantity = 0L;
        dateManufactured = new Timestamp(new Date().getTime());
        dateShelf = new Timestamp((new Date().getTime())+ (365 * 86400000)*(10/12));
        datePrinted = new Timestamp((new Date().getTime())+(365 * 86400000)*(11/12));
        dateExpired = new Timestamp((new Date().getTime())+365 * 86400000); //dateExpired is one year after manufacturing date
        inventory = new ProductInventoryRecord();
        reservations = new ArrayList<InventoryReservation>();
        locations = new ArrayList<WarehouseLocation>();
    }
    
    private String generateBatchNo() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        String frontId = sdf.format(cal.getTime());
        return frontId;
    }
    
    public void replaceBatchNoWithAlpha(String productType) {
        if (productType.equals(ProductType.BAR_A.getText())) {
            this.batchNo = generateBatchNo()+ "A";
        } else if (productType.equals(ProductType.BAR_B.getText())) {
            this.batchNo = generateBatchNo()+ "B";
        } else if (productType.equals(ProductType.BAR_C.getText())) {
            this.batchNo = generateBatchNo()+ "C";
        } else if (productType.equals(ProductType.BAR_D.getText())) {
            this.batchNo = generateBatchNo()+ "D";
        }
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public List<WarehouseLocation> getLocations() {
        return locations;
    }

    public void setLocations(List<WarehouseLocation> locations) {
        this.locations = locations;
    }
    
    public List<InventoryReservation> getReservations() {
        return reservations;
    }

    public void setReservations(List<InventoryReservation> reservations) {
        this.reservations = reservations;
    }
    
    
    public ProductInventoryRecord getInventory() {
        return inventory;
    }

    public void setInventory(ProductInventoryRecord inventory) {
        this.inventory = inventory;
    }
    
    public String getBatchNo() {
        return batchNo;
    }

    public void setBatchNo(String batchNo) {
        this.batchNo = batchNo;
    }

    public Timestamp getDateExpired() {
        return dateExpired;
    }

    public void setDateExpired(Timestamp dateExpired) {
        this.dateExpired = dateExpired;
    }

    public Timestamp getDateManufactured() {
        return dateManufactured;
    }

    public void setDateManufactured(Timestamp dateManufactured) {
        this.dateManufactured = dateManufactured;
    }

    public Long getQuantity() {
        return quantity;
    }

    public void setQuantity(Long quantity) {
        this.quantity = quantity;
    }

    public Double getBasePrice() {
        return basePrice;
    }

    public void setBasePrice(Double basePrice) {
        this.basePrice = basePrice;
    }

    public Timestamp getDatePrinted() {
        return datePrinted;
    }

    public void setDatePrinted(Timestamp datePrinted) {
        this.datePrinted = datePrinted;
    }

    public Timestamp getDateShelf() {
        return dateShelf;
    }

    public void setDateShelf(Timestamp dateShelf) {
        this.dateShelf = dateShelf;
    }
    
    

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof FinishedGood)) {
            return false;
        }
        FinishedGood other = (FinishedGood) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.mrp.pmm.entity.FinishedGood[ id=" + id + " ]";
    }
    
}
